package cm.android;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.R.string;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;


public class EnumFiles extends Activity implements OnItemClickListener {
	
	private String mParentRoot = null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enumfolder);
        ListView ls = (ListView) findViewById(R.id.ListView01);  
        
        ls.setLongClickable(true);
        ls.setOnItemClickListener(this);
        registerForContextMenu(ls);
        
        EnumSDCardFolder("/sdcard");
        mParentRoot = "/sdcard";
	}
	
	private void EnumSDCardFolder(String strPath) {
		ListView ls = (ListView) findViewById(R.id.ListView01);
		 //生成动态数组，加入数据  
        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
        
        HashMap<String, Object> map = new HashMap<String, Object>();  
        map.put("ItemImage", R.drawable.folder); 
        map.put("ItemTitle", "...");  
        map.put("ItemText", "up to parent folder");
        listItem.add(map);
        
        File file = new File(strPath);
    	File[] files = file.listFiles();
        if (files != null) {
	    	for (File f : files) {
	    		if (f.isDirectory()) {
			        map = new HashMap<String, Object>();  
			        map.put("ItemImage", R.drawable.checked); 
			        map.put("ItemTitle", f.getAbsolutePath());  
			        map.put("ItemText", "");
			        listItem.add(map);  
		        } 
	    	}
	    }

        SimpleAdapter listItemAdapter = new SimpleAdapter(
        		this,listItem,// 数据源   
        		R.layout.folderitemview,
        		new String[] {"ItemImage","ItemTitle", "ItemText"},   
        		//ImageItem的XML文件里面的一个ImageView,两个TextView ID  
        		new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}  
        );  

        //添加并且显示  
        ls.setAdapter(listItemAdapter);  
	}
	
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("Add Sync Path");
		menu.add(0, 0, 0, "Add Sync Path");
	}
	
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo ac = (AdapterContextMenuInfo)item.getMenuInfo();
		ListView ls = (ListView) findViewById(R.id.ListView01);
		HashMap<String, Object> map = (HashMap<String, Object>)ls.getAdapter().getItem(ac.position);
		String string = map.get("ItemTitle").toString();
		Intent it = new Intent();
		it.putExtra("folder", string);
		setResult(RESULT_OK, it);
		finish();
		return super.onContextItemSelected(item);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
		if (arg2 == 0) {
			EnumSDCardFolder(mParentRoot);
		} else {
			HashMap<String, Object> object = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
			String string = object.get("ItemTitle").toString();
			EnumSDCardFolder(string);
			mParentRoot = string.substring(0, string.lastIndexOf("/"));
		}
		
	}

}
